<template>
  <div class="login-container">
    <el-form
        :model="form"
        :rules="rules"
        ref="loginForm"
        label-width="80px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
            v-model="form.username"
            placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
            v-model="form.password"
            placeholder="请输入密码"
            type="password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            @click="submitForm('loginForm')"
        >登录</el-button>
        <el-button
            @click="resetForm('loginForm')"
        >重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import axios     from "axios";
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 这里可以添加实际的登录逻辑，比如发送请求到后端验证用户名和密码
          axios.post('http://localhost:8781/admin/login', this.form).then(res=>{
            console.log(res)
            if(res.data.code==200){
              localStorage.setItem('loginInfo', JSON.stringify(res.data.data));
              this.$router.push('/');
            }else{
              this.$message.error(res.data.msg);
            }

          })
          console.log('登录成功');
        } else {
          console.log('表单验证失败，请检查输入信息');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.login-container {
  width: 300px;
  margin: 100px auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  background-color: #f9f9f9;
}
</style>